package com.suning.app.snpaasadmin.api;

import com.suning.app.snpaasadmin.domain.OauthToken;
import com.suning.app.snpaasadmin.util.CloudFoundryClientUtil;
import com.suning.app.snpaasadmin.util.JsonUtil;

import java.util.LinkedHashMap;
import java.util.Map;


public class OauthApi {
	
	public static OauthToken login(String username,String password,String domain) throws Exception{
		//构造参数
		Map<String,String> params=new LinkedHashMap<String,String>();
		params.put("grant_type", "password");
		params.put("username", username);
		params.put("password", password);
		
		String host = "http://uaa." + domain;
		String response=CloudFoundryClientUtil.doOauth(host+"/oauth/token", params,"uaa."+domain);
		Map<String,Object> data=(Map<String, Object>) JsonUtil.convertJsonToMap(response);
		return new OauthToken((String)data.get("access_token"),(String)data.get("token_type"),(String)data.get("refresh_token"),(int)data.get("expires_in"));
	}
}
